<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	//获取菜单数据
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择套餐！','error');
	}
	//获取系统菜单
	$framess=core_setting('menu');
	foreach($framess as $key1=>&$menu){
			if($menu['hide']){
				unset($framess[$key1]);
				continue;
			}
			foreach($menu['child'] as $key2=>&$sub){
				if($sub['hide']){
					unset($menu['child'][$key2]);
					continue;
				}
				foreach($sub['child'] as $key3=>&$row){
					if($row['hide']){
						unset($sub['child'][$key3]);
						continue;
					}
					if($row['url']['plugin']=='core'){
						$permission=pdo_get('core_permissions',array('action'=>$row['url']['action'],'op'=>$row['url']['op']),array('id','type'));
						if(!$permission){
							$permission=pdo_get('core_permissions',array('action'=>$row['url']['action'],'op'=>''),array('id','type'));
						}
						//type字段值说明：如果查不到相应权限集数据，则表示无需登陆即可访问；type=0表示登陆即可访问；type=1以上表示需要登陆且拥有相应权限方可访问
						if($permission){
							if(!$_SESSION['uid']){
								unset($sub['child'][$key3]);
								continue;
							}
							$row['type']=$permission['type'];
							if($permission['type']>3){
								unset($sub['child'][$key3]);
								continue;
							}
						}
					}
					$menu['url2']=$menu['url2']?:$row['url'];
				}
				if(!$sub['child']){
					unset($menu['child'][$key2]);
				}
			}
			$check1=true;
			if($menu['url']['plugin']=='core'){
				$permission=pdo_get('core_permissions',array('action'=>$menu['url']['action'],'op'=>$menu['url']['op']),array('id','type'));
				if(!$permission){
					$permission=pdo_get('core_permissions',array('action'=>$menu['url']['action'],'op'=>''),array('id','type'));
				}
				//type字段值说明：如果查不到相应权限集数据，则表示无需登陆即可访问；type=0表示登陆即可访问；type=1以上表示需要登陆且拥有相应权限方可访问
				if($permission){
					if(!$_SESSION['uid']){
						$check1=false;
					}
					$menu['type']=$permission['type'];
					if($permission['type']>3){
						$check1=false;
					}
				}
			}
			if(!$menu['child'] && (!$menu['url'] || !$check1)){
				unset($framess[$key1]);
			}elseif(!$check1 && $menu['url']){
				$menu['url']=$menu['url2'];
			}
	}
	$framess=array_values($framess);
	$permissions=pdo_get('core_account_group',array('id'=>$id),array('menus','othermenus'));
	$menus=$permissions['menus'];
	if($menus){
		$menus=json_decode($menus,true);
	}else{
		$menus=$framess;
	}
	$return['menus']=$menus;
	$othermenus=$permissions['othermenus'];
	if($othermenus){
		$othermenus=json_decode($othermenus,true);
	}else{
		$othermenus=array(
			array('title'=>'我的场景'),
			array('title'=>'我的应用'),
		);
	}
	$return['othermenus']=$othermenus;
	$return['id']=$id;
	$return['htmls']=file_get_contents(ROOT_D.'/manage/templates/default/windows/account/groupmenu.html');
	exi($return);
}
if($_RQ['op']=='choose_plugin'){
	if($_RQ['id']){
		$plugin=pdo_get('core_plugins',array('id'=>intval($_RQ['id'])),array('name','menus','covers','title','type'));
			if($plugin['type']!=4){
				$is_new=1;
			}else{
				$is_new=0;
			}
			$list=array();
			$list[]=array('title'=>'插件首页','url'=>array('plugin'=>$plugin['name'],'action'=>'basic.module_home'));
			if($plugin['menus']){
				$plugin['menus']=json_decode($plugin['menus'],true);
				foreach($plugin['menus'] as $menu){
					if($plugin['type']!=4){
						$list[]=array('title'=>$menu['title'],'url'=>array('plugin'=>$plugin['name'],'action'=>$menu['name'],'title'=>$menu['title']));
					}else{
						$list[]=array('title'=>$menu['title'],'url'=>'./index.php?action=module&do='.$menu['name'].'&m='.$plugin['name']);
					}
					
				}
			}
			if($plugin['covers']){
				$plugin['covers']=json_decode($plugin['covers'],true);
				foreach($plugin['covers'] as $menu){
					$list[]=array('title'=>$menu['title'].'入口设置','url'=>array('plugin'=>$plugin['name'],'action'=>'basic.module_home','op'=>'cover','query'=>'is_new='.$is_new.'&ac='.$menu['name'],'title'=>$menu['title'].'入口设置'));
				}
			}
			
			$return['list']=$list?$list:array();
			$return['htmls']=array(
				'type'=>'list',
				'tabs'=>array(
					array('op'=>'choose','title'=>'系统功能'),
					array('op'=>'choose_plugin','title'=>'插件功能'),
				),
				'list'=>array('data'=>'list','delall'=>false,'edit'=>false,'del_one'=>false,'pagelist'=>false,'list'=>array(
					array('name'=>'title','title'=>'页面名称','type'=>'string'),
				),'action'=>array(
					array('type'=>'modal_result','name'=>'url','title'=>'选取'),
				)),
				'search_one'=>array('name'=>'title','add'=>false),
			);
			exi($return);
	}else{
		$pindex=max(1, intval($_RQ['page']));
			$psize=15;
			$params=array();
			if($_RQ['title']){
				$return['params']['title']=$_RQ['title'];
				$params['title like']='%'.$_RQ['title'].'%';
			}
			$list=pdo_getslice('core_plugins',$params,array($pindex,$psize),$total,array('id','title'));
			$return['list']=$list?$list:array();
			$totalpage=ceil($total/$psize);
			$totalpage=intval($totalpage);
			$return['totalpage'] =$totalpage;
			$return['params']['page'] =$pindex;
			$return['htmls']=array(
				'type'=>'list',
				'tabs'=>array(
					array('op'=>'choose','title'=>'系统功能'),
					array('op'=>'choose_plugin','title'=>'插件功能'),
					array('op'=>'diy_url','title'=>'自定义'),
				),
				'list'=>array('data'=>'list','delall'=>false,'edit'=>false,'del_one'=>false,'list'=>array(
					array('name'=>'title','title'=>'插件名称','type'=>'string'),
				),'action'=>array(
					array('type'=>'modal','op'=>'choose_plugin','title'=>'选择功能'),
				)),
				'search_one'=>array('name'=>'title','add'=>false),
			);
			exi($return);
	}
}
if($_RQ['op']=='choose'){
	$pindex=max(1, intval($_RQ['page']));
			$psize=15;
			$params=array();
			if($_RQ['title']){
				$return['params']['title']=$_RQ['title'];
				$params['title like']='%'.$_RQ['title'].'%';
			}
			$list=pdo_getslice('core_permissions',$params,array($pindex,$psize),$total,array('id','action','op','title'));
			if($list){
				foreach($list as &$row){
					$row=array('title'=>$row['title'],'url'=>array('plugin'=>'core','action'=>$row['action'],'op'=>$row['op'],'title'=>$row['title']));
				}
			}
			$return['list']=$list?$list:array();
			$totalpage=ceil($total/$psize);
			$totalpage=intval($totalpage);
			$return['totalpage'] =$totalpage;
			$return['params']['page'] =$pindex;
			$return['htmls']=array(
				'type'=>'list',
				'tabs'=>array(
					array('op'=>'choose','title'=>'系统功能'),
					array('op'=>'choose_plugin','title'=>'插件功能'),
					array('op'=>'diy_url','title'=>'自定义'),
				),
				'list'=>array('data'=>'list','delall'=>false,'edit'=>false,'del_one'=>false,'list'=>array(
					array('name'=>'title','title'=>'页面名称','type'=>'string'),
				),'action'=>array(
					array('type'=>'modal_result','name'=>'url','title'=>'选取'),
				)),
				'search_one'=>array('name'=>'title','add'=>false),
			);
			exi($return);
}
if($_RQ['op']=='diy_url'){
	$return=array();	
	$return['htmls']=array(
		'type'=>'edit',
		'post'=>'modal_result',
		'desc'=>'自定义链接页面',
		'tabs'=>array(
			array('op'=>'choose','title'=>'系统功能'),
			array('op'=>'choose_plugin','title'=>'插件功能'),
			array('op'=>'diy_url','title'=>'自定义'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'plugin','title'=>'plugin','help'=>'如果是插件页面，填写插件标识。如果是系统页面，填写‘core’'),
			array('type'=>'string','name'=>'action','title'=>'action','help'=>'填写要访问的页面的功能标识'),
			array('type'=>'string','name'=>'op','title'=>'op','help'=>'如果是访问页面特定op，这里填写op标识'),
			array('type'=>'string','name'=>'query','title'=>'query','help'=>'如果需要携带额外请求参数，请在这里设置。以key1=value1&key2=value2...的形式'),
		)
	);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//根据排序调整菜单顺序
	$othermenus=$postdata['othermenus'];
	$menus=$postdata['menus'];
	$id=$postdata['id'];
	if(!$id){
		exi('请选择套餐！','error');
	}
	paixu($menus,'paixu');
	foreach($menus as &$menu){
		if($menu['show_child']){
			unset($menu['show_child']);
		}
		if(is_array($menu['child'])){
			foreach($menu['child'] as &$sub){
				if($sub['show_child']){
					unset($sub['show_child']);
				}
				if(is_array($sub['child'])){
					foreach($sub['child'] as &$row){
						if(!$row['action'] && !$row['url']){
							exi('菜单：'.$row['title'].'未设置动作！','error');
						}
					}
				}
				if(is_array($sub['child']) && count($sub['child'])>1){
					paixu($sub['child'],'paixu');
				}
			}
		}
		if(count($menu['child'])>1){
			paixu($menu['child'],'paixu');
		}
	}
	
	pdo_update('core_account_group',array('menus'=>ijson_encode($menus),'othermenus'=>ijson_encode($othermenus)),array('id'=>$id));
	exi('系统菜单设置成功！','','goback');
}
if($_RQ['op']=='deleall'){
	$postdata=get_postdata();
	$id=$postdata['id'];
	if(!$id){
		exi('请选择套餐！','error');
	}
	pdo_update('core_account_group',array('menus'=>'','othermenus'=>''),array('id'=>$id));
	exi('重置菜单成功！');
}
?>